Skip to content

Speed Up Sync Cog Loading#2148

Merged
ChrisLovering merged 3 commits into
mainfrom
speedup-syncing
Apr 25, 2022
Merged

Speed Up Sync Cog Loading#2148
ChrisLovering merged 3 commits into
mainfrom
speedup-syncing

Conversation

@HassanAbouelela
Copy link
Copy Markdown
Contributor

The user syncer was blocking the startup of the sync cog due to having to perform thousands of pointless member fetch requests. This speeds up that process b:

  • Increasing the probability that the cache is up-to-date using Guild.chunked, and assuming that any found cache data is correct
  • Limiting the fetches to members who were in the guild during the previous sync only, meaning we don't fetch a bunch of outdated members

The user syncer was blocking the startup of the sync cog due to having
to perform thousands of pointless member fetch requests. This speeds up
that process by increasing the probability that the cache is up-to-date
using `Guild.chunked`, and limiting the fetches to members who were in
the guild during the previous sync only.

Co-authored-by: ChrisJL <chrislovering@users.noreply.github.com>
Co-authored-by: wookie184 <wookie1840@gmail.com>
Signed-off-by: Hassan Abouelela <hassan@hassanamr.com>
@HassanAbouelela HassanAbouelela added t: bug Something isn't working a: backend Related to internal functionality and utilities (error_handler, logging, security, utils and core) a: API Related to or causes API changes p: 1 - high High Priority labels Apr 23, 2022
Copy link
Copy Markdown
Member

@ChrisLovering ChrisLovering left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 🚀

Comment thread bot/exts/backend/sync/_cog.py Outdated
Comment on lines +31 to +34
log.info("Waiting for guild to be chunked to start syncers.")
while not guild.chunked:
await asyncio.sleep(10)
log.info("Starting syncers.")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this guaranteed to succeed? If not, there should be some time out and the syncer should still run if possible.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only time this won't succeed is if the member cache isn't populated by Discord.py, which could happen in a very rare case if Discord has gateway issues.

In that case we wouldn't want the user syncer to run at all, as we assume further down if a user isn't in the member cache, they are no longer a member.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does a 30 minute timeout sound reasonable?

Copy link
Copy Markdown
Member

@ChrisLovering ChrisLovering Apr 25, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

30 minutes would be ample time for this to succeed.

in the case where this times out I think the correct thing to do would be to log a warning/error and cancel the cog load.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't sound like the syncer should run in that case. Thus, it might as well wait forever, unless you think some log message would be useful. I think wait_until_guild_available already checks if the caches are populated anyway (I assume the syncer cog is waiting for that somewhere).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added an exception and timeout in acc1654. It will now log the following after 30 minutes:

discord.ext.commands.errors.ExtensionFailed: Extension 'Sync' raised an error: RuntimeError: The guild was not chunked in time, not loading sync cog.

Copy link
Copy Markdown
Contributor

@MarkKoz MarkKoz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if the member was not in the cache and in_guild = False. Does this mean the DB won't be able to know that they've rejoined the server (until the next sync where hopefully they are in cache)?

@HassanAbouelela
Copy link
Copy Markdown
Contributor Author

The alternative would be fetching thousands of members (up until it was purged 200-300k). That doesn't seem feasible anymore.

@MarkKoz
Copy link
Copy Markdown
Contributor

MarkKoz commented Apr 25, 2022

What kind of impact could a member being out of sync have on the rest of our features?

@HassanAbouelela
Copy link
Copy Markdown
Contributor Author

HassanAbouelela commented Apr 25, 2022

I think the big impact would primarily be on moderation tools. More specifically, the user embed would be off.

There are some other operations that would have problems if the cache was off (banning would raise an error for instance), but that is only in the case where the user is not in the guild, but in the cache. I don't think that's an anticipated scenario, the opposite is more likely.

HassanAbouelela and others added 2 commits April 25, 2022 21:33
Adds a 30-minute timeout while waiting for the guild to be chunked in
the sync cog, after which the cog is not loaded.

Signed-off-by: Hassan Abouelela <hassan@hassanamr.com>
@ChrisLovering ChrisLovering enabled auto-merge April 25, 2022 17:51
@ChrisLovering ChrisLovering merged commit 9b92876 into main Apr 25, 2022
@ChrisLovering ChrisLovering deleted the speedup-syncing branch April 25, 2022 17:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

a: API Related to or causes API changes a: backend Related to internal functionality and utilities (error_handler, logging, security, utils and core) p: 1 - high High Priority t: bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants